﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dhgms.TplHelper.Model.Info.TaskResult
{
    using System.Data;
    using System.Data.Common;

    using Dhgms.DataManager.Model.Info;

    /// <summary>
    /// Represents a task that returns a strongly typed record
    /// </summary>
    /// <typeparam name="TInformationClass">
    /// The information class for the strongly typed record
    /// </typeparam>
    public class SingleStronglyTyped<TInformationClass>
        : TaskResultBase // ReSharper restore RedundantNameQualifier
        where TInformationClass : new()
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StronglyTypedCollection{TInformationClass}"/> class. 
        /// </summary>
        public SingleStronglyTyped()
        {
        }

        // ********** WARNING **********
        // This code is automatically generated! Any Changes you make to this file will be lost!
        // To make changes edit the corresponding .tt file!

        /// <summary>
        /// Initializes a new instance of the <see cref="SingleStronglyTyped{TInformationClass}"/> class. 
        /// Constructor
        /// </summary>
        /// <param name="name">
        /// Name of the task
        /// </param>
        /// <param name="started">
        /// Timestamp of when the task started
        /// </param>
        /// <param name="finished">
        /// Timestamp of when the task finished
        /// </param>
        /// <param name="result">
        /// The result for the task
        /// </param>
        public SingleStronglyTyped(
            string name, DateTime started, DateTime finished, TInformationClass result)
            : base(name, started, finished)
        {
            this.Result = result;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets a header record for use for something like a CSV file
        /// </summary>
        /// <returns>a collection of strings representing the header record</returns>
        public override IList<string> HeaderRecord
        {
            get
            {
                var result = new List<string>(base.HeaderRecord) { "Result" };

                return result;
            }
        }

        /// <summary>
        /// The result for the task
        /// </summary>
        public TInformationClass Result { get; set; }

        #endregion

        // ********** WARNING **********
        // This code is automatically generated! Any Changes you make to this file will be lost!
        // To make changes edit the corresponding .tt file!
        #region Public Methods and Operators

        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="other">
        /// The other.
        /// </param>
        /// <return>
        /// 0 if equal, otherwise non zero
        /// </return>
        /// <returns>
        /// The compare to.
        /// </returns>
        public int CompareTo(TInformationClass other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            int checkResult = this.CompareTo(other);
            return checkResult != 0 ? checkResult : 0;

            // Result
            // checkResult = Result.CompareTo(other.Result);

            // if(checkResult != 0)
            // {
            // return (checkResult > 0) ? 4 : -4;
            // }
        }

        /// <summary>
        /// Checks a table to ensure it meets the required schema
        /// </summary>
        public override void DoTableValidation()
        {
            base.DoTableValidation();

            // Result
        }

        /// <summary>
        /// Checks if the current instance matches another of the same type
        /// </summary>
        /// <param name="other">
        /// object to compare
        /// </param>
        /// <returns>
        /// true if equal, otherwise false
        /// </returns>
        public bool Equals(SingleStronglyTyped<TInformationClass> other)
        {
            return this.CompareTo(other) == 0;
        }

        /// <summary>
        /// Checks this instance against another to see where there are differences
        /// </summary>
        /// <param name="other">
        /// other instance to compare
        /// </param>
        /// <returns>
        /// summary of where there are differences
        /// </returns>
        public StronglyTypedCollection GetDifferences(
            // ReSharper restore RedundantNameQualifier
            SingleStronglyTyped<TInformationClass> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            // Name
            int checkResult = string.CompareOrdinal(this.Name, other.Name);

            bool name = checkResult != 0;

            // Started
            checkResult = this.Started.CompareTo(other.Started);

            bool started = checkResult != 0;

            // Finished
            checkResult = this.Finished.CompareTo(other.Finished);

            bool finished = checkResult != 0;

            // Result
            /*
            if (Result != null)
            {
                if (other.Result != null)
                {
                    checkResult = -1;
                }
                else
                {
                    checkResult = Result.CompareTo(other.Result);
                }
            }
            else if (other.Result != null)
            {
                checkResult = 1;
            }
             * */
            bool result = checkResult != 0;

            // ReSharper disable RedundantNameQualifier
            return new StronglyTypedCollection(
                // ReSharper restore RedundantNameQualifier
                name, started, finished, result);
        }

        // ********** WARNING **********
        // This code is automatically generated! Any Changes you make to this file will be lost!
        // To make changes edit the corresponding .tt file!

        /// <summary>
        /// Gets the hash code for the object
        /// </summary>
        /// <returns>
        /// hash code
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode() ^ this.Result.GetHashCode();
        }

        /// <summary>
        /// Gets a collection of string data for use for something like a CSV file
        /// </summary>
        /// <returns>
        /// a collection of strings representing the data record
        /// </returns>
        public override IList<string> ToStringArray()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
